package com.kingtone.rongji;

import com.kingtone.rongji.pay.alipay.AliPayActivity;
import com.kingtone.rongji.pay.weixin.WeiXinPayActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PayNoticeActivity extends BaseActivity {

	private TextView title = null;
	private ImageView backBtn = null;
	private TextView payCount = null;
	private LinearLayout weixinLayout = null;
	private LinearLayout aliPayLayout = null;
	private LinearLayout xianxiangPayLayout = null;
	private LinearLayout yinxinPayLayout = null;
	private double payAmount = 0.0;
	private String orderNo = null;
	private String subject = null;
	private String body = null;
	private Activity mContext;
	private int orderType;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_pay_notice);
		this.mContext = this;
		Intent intent = getIntent();
		if(intent.hasExtra("payAmount"))
		{
			payAmount = intent.getDoubleExtra("payAmount", 0.0);
		}
		if(intent.hasExtra("orderNo"))
		{
			orderNo = intent.getStringExtra("orderNo");
		}
		if(intent.hasExtra("subject"))
		{
			subject = intent.getStringExtra("subject");
		}
		if(intent.hasExtra("body"))
		{
			body = intent.getStringExtra("body");
		}
		if(intent.hasExtra("orderType"))
		{
			orderType = intent.getIntExtra("orderType", 0);
		}
		
		setupView();
	}
	
	private void setupView()
	{
		title = (TextView)findViewById(R.id.title);
		title.setText(getString(R.string.pay_title));
		backBtn = (ImageView)findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				onBackPressed();
			}
		});
		
		payCount = (TextView)findViewById(R.id.pay_account);
		payCount.setText(Html.fromHtml(getString(R.string.pay_count) +"<font color='#FF0000'>￥ "+ payAmount + "</font>"));
		weixinLayout = (LinearLayout)findViewById(R.id.pay_wei_xin);
		weixinLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//微信支付
				Intent intent = new Intent(PayNoticeActivity.this, WeiXinPayActivity.class);
    			if(TextUtils.isEmpty(body))
    			{
    				body = subject;
    			}
    			intent.putExtra("body", body);
    			intent.putExtra("price", payAmount);
    			startActivity(intent);
			}
		});
		
		aliPayLayout = (LinearLayout)findViewById(R.id.pay_ali_pay);
		aliPayLayout.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//调用支付宝支付接口
    			Intent intent = new Intent(PayNoticeActivity.this, AliPayActivity.class);
    			intent.putExtra("subject", subject);
    			if(TextUtils.isEmpty(body))
    			{
    				body = subject;
    			}
    			intent.putExtra("body", body);
    			intent.putExtra("price", payAmount);
    			startActivityForResult(intent, 200);
			}
			
		});
		xianxiangPayLayout = (LinearLayout)findViewById(R.id.pay_xianxiang_pay);
		xianxiangPayLayout.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(PayNoticeActivity.this, XianXiangPayActivity.class);
				intent.putExtra("orderNo", orderNo);
				intent.putExtra("orderType", orderType);
				startActivity(intent);	
			}
		});
		yinxinPayLayout = (LinearLayout)findViewById(R.id.pay_yinxin_pay);
		yinxinPayLayout.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(PayNoticeActivity.this, YinXinPayActivity.class);
				intent.putExtra("orderNo", orderNo);
				intent.putExtra("orderType", orderType);
				startActivity(intent);
				
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
	  if(resultCode == RESULT_OK){
			Bundle bundle = data.getExtras();
			if(bundle != null)
			{
				String resultStatus = "";
				if(bundle.containsKey("resultStatus") && bundle.get("resultStatus") != null)
				{
					resultStatus = bundle.get("resultStatus").toString();
				}
				String resultMsg = "支付失败，请稍候再试!";
				if(!TextUtils.isEmpty(resultStatus) && resultStatus.equals("9000"))
				{
					Toast.makeText(mContext, "支付成功!", Toast.LENGTH_SHORT).show();
					if(orderType == 6)
					{
						//加载WebView
						Intent intent = new Intent(mContext, SendFriendsActivity.class);
						intent.putExtra("orderNum", orderNo);
						startActivity(intent);
					}else
					{
						//支付成功，返回订单详情页面
						Intent intent = new Intent(PayNoticeActivity.this, OrderDetailActivity.class);
						intent.putExtra("orderNum", orderNo);
						startActivity(intent);
					}
				}else
				{
					if(bundle.containsKey("resultMsg") && bundle.get("resultMsg") != null)
					{
						resultMsg = bundle.get("resultMsg").toString();
					}
					Toast.makeText(mContext, resultMsg, Toast.LENGTH_SHORT).show();
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
